const userModel = require("../models/userModel");
async function paginate(model, page, pageSize, rest) {
  try {
    const skip = (page - 1) * pageSize;
    const limit = pageSize;
    // 查询数据库并使用 skip 和 limit 进行分页
    const result = await model.find(rest).skip(skip).limit(limit);
    const totalCount = await model.countDocuments({});
    return {
      list: result,
      total: totalCount,
    };
  } catch (error) {
    console.error("Pagination error:", error);
    throw error;
  }
}
const user = {
  addUser: async (body) => {
    return new userModel(body);
  },
  findList: async (body) => {
    const { currentPage, pageSize, ...rest } = body;
    return paginate(userModel, currentPage, pageSize, rest);
  },
};
module.exports = user;
